package com.woniuxy.ticketcinema.exception;

import com.woniuxy.ticketcommon.entity.ResponseResult;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 */
@RestControllerAdvice
public class ArgExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult<Map> addException(MethodArgumentNotValidException e){

        BindingResult bindingResult = e.getBindingResult();

        Map<String, String> msg = new HashMap<>();
        bindingResult.getFieldErrors().forEach(c -> msg.put(c.getField(), c.getDefaultMessage()));

        ResponseResult<Map> responseResult = new ResponseResult<>();
        responseResult.setCode(102);
        responseResult.setMsg("无效参数");
        responseResult.setData(msg);
        return responseResult;
    }
}
